package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.AdminInfoSonVo;
import com.cskaoyan.bean.vo.AdminLogBo;
import com.cskaoyan.bean.vo.AdminLogListVo;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.LogMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    LogMapper logMapper;

    @Override
    public Map<String, Object> listAdmin(Integer page, Integer limit, String sort, String order, String username) {

        PageHelper.startPage(page,limit);
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(sort + " " +  order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        ArrayList<ListAdminVO> list = new ArrayList<>();
        for (Admin admin : admins) {

            ListAdminVO listAdminVO = new ListAdminVO(admin.getId(),
                                                    admin.getUsername(),
                                                    admin.getAvatar(),
                                                    admin.getRoleIds()
                                                    );
            list.add(listAdminVO);
        }
        HashMap<String, Object> map = new HashMap<>();
        int total = list.size();
        map.put("total",total);
        map.put("items",list);
        return map;
    }

    @Override
    public Map<String, Object> createAdmin(Admin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        adminMapper.insertSelective(admin);
        Map map = putInMap(admin);
        return map;
    }

    @Override
    public Map<String, Object> updateAdmin(Admin admin) {
        admin.setUpdateTime(new Date());
        adminMapper.updateByPrimaryKey(admin);
        return putInMap(admin);
    }

    @Override
    public Integer deleteAdmin(Admin admin) {
        admin.setUpdateTime(new Date());
        admin.setDeleted(true);
        int update = adminMapper.updateByPrimaryKeySelective(admin);
        return update;
    }


    @Override
    public AdminLogListVo queryAdminLogList(String name, Integer page, Integer limit, String sort, String order) {
//        开启分页插件
        PageHelper.startPage(page, limit);
        //设置分页条件

        LogExample logExample = new LogExample();

        LogExample.Criteria criteria = logExample.createCriteria();
        logExample.setOrderByClause(sort + " " + order);
        if (null != name && !("".equals(name))) {
            criteria.andAdminLike(name);
        }
        criteria.andDeletedEqualTo(false);
        List<AdminLogBo> adminLogBos = adminMapper.queryAdminLogListByPage(logExample);

//        查询总数据
        PageInfo<AdminLogBo> adminPoPageInfo = new PageInfo<AdminLogBo>(adminLogBos);
        long total = adminPoPageInfo.getTotal();

        AdminLogListVo adminLogListVo = new AdminLogListVo(total, adminLogBos);
        //返回list
        return adminLogListVo;
    }

    private Map<String, Object> putInMap(Admin admin) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",admin.getId());
        map.put("username",admin.getUsername());
        map.put("password",admin.getPassword());
        map.put("avatar",admin.getAvatar());
        map.put("addTime",admin.getAddTime());
        map.put("updateTime",admin.getUpdateTime());
        map.put("roleIds",admin.getRoleIds());
        return map;
    }


    @Override
    public List<String> selectPasswordByUsername(String username) {
        List<String> strings = adminMapper.selectPasswordByUsername(username);
        return strings;
    }

    @Override
    public List<String> selectRoleIds(String username) {
        Admin admin = adminMapper.selectRoleIds(username);
        Integer[] roleIds = admin.getRoleIds();
        List<String> permissions= new LinkedList<>();
        for (Integer id : roleIds) {
            List<String> strings = adminMapper.selectPermissionsByRoleId(id);
            for (String string : strings) {
                permissions.add(string);
            }
        }
        return permissions;
    }

    @Override
    public AdminInfo selectAdminInfo(String username) {
        AdminInfo info = new AdminInfo();
        AdminInfoSonVo adminInfo =adminMapper.selectNameAndAvatar(username);
        info.setName(adminInfo.getName());
        info.setAvatar(adminInfo.getAvatar());
        Integer[] roleIds = adminInfo.getRoleIds();
        List<String> roles=adminMapper.selectRoles(roleIds);
        info.setRoles(roles);

        List<String> permissions =new ArrayList<>();
        List<String> permission =new ArrayList<>();
        for (Integer roleId : roleIds) {
            List<String> strings = adminMapper.selectPermissionsByRoleId(roleId);
            permissions.addAll(strings);
        }
        for (String s : permissions) {
            if (s.equals("*")) {
                permissions.clear();
                permission.add("*");
                break;
            }
            List<String> lists = adminMapper.selectRolesPermissionsByPermission(s);
            permission.addAll(lists);
        }
        info.setPerms(permission);

        return info;
    }

    @Override
    public void insertlog(String username, String ip,String action) {

        Log record = new Log(null,username,ip,1,action,true,"","",new Date(),new Date(),false);
        logMapper.insertSelective(record);
    }

}

